<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('auth.admin.username') + '/' + t('auth.admin.nickname') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table />

        <!-- 表单 -->
        <PopupForm />
    </div>
</template>

<script setup lang="ts">
import { provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useI18n } from 'vue-i18n'
import createAxios from '/@/utils/axios'

defineOptions({
    name: 'goods/goods',
})

const { t } = useI18n()
const optButtons = defaultOptButtons(['edit', 'delete'])


const baTable = new baTableClass(
    new baTableApi('/backend/goods/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 60 },
            { label: t('goods.goods.name'), prop: 'name', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query'), width: 420 },
            {
                label: t('goods.goods.cate'), prop: 'cate_id', align: 'center', operator: 'LIKE',
                operatorPlaceholder: t('Fuzzy query'),
                formatter(row, column, cellValue, index) {
                    //console.log('自定义列显示', row, column, cellValue, index)
                    // return row.cate?.title + '（id：'+ cellValue +'）'
                    return row.cate?.title
                }
            },
            { label: t('goods.goods.price'), prop: 'price', align: 'center', operator: false, render: 'tags' },
            { label: t('goods.goods.market_price'), prop: 'market_price', align: 'center', operator: false, render: 'tags' },
            { label: t('goods.goods.pic'), prop: 'pic', align: 'center', render: 'image', operator: false },
            {
                label: t('goods.goods.is_short_sale'),
                prop: 'is_short_sale',
                align: 'center',
                render: 'tag',
                custom: { disable: 'danger', enable: 'success' },
                replaceValue: { '0': t('goods.goods.no'), '1': t('goods.goods.yes')  },
            },
            {
                label: t('goods.goods.is_start'),
                prop: 'is_start',
                align: 'center',
                render: 'tag',
                custom: { disable: 'danger', enable: 'success' },
                replaceValue: { '0': t('goods.goods.no'), '1': t('goods.goods.yes')  },
            },
            { label: t('Create time'), prop: 'ctime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { '0': 'danger', '1': 'success' },
                replaceValue: { '0': t('Disable'), '1': t('Enable') },
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined, 'status'],
    },
    {
        defaultItems: {
            status: '1',
        },
    },
    {
        // before
    },
    {
        // after
        // 切换表单后
        toggleForm({ operate }) {
            if (operate == 'Show') {
                menuRuleTreeUpdate()
            }
            if (operate == 'Edit') {
                menuRuleTreeUpdate()
            }
            if (operate == 'Add') {
                menuRuleTreeUpdate()
            }
        },
        // 编辑请求完成后
        requestEdit() {
            menuRuleTreeUpdate()
        },
        // 编辑请求完成后
        requestShow() {
            menuRuleTreeUpdate()
        },
    }
)

provide('baTable', baTable)

baTable.mount()
baTable.getLists()

// 获取角色
const menuRuleTreeUpdate = () => {
    createAxios({
        url: '/backend/goods-cate/tree',
        method: 'post',
        params: { isTree: true },
    }).then((res) => {
        console.log('弹出修改前', baTable.form.items)

        baTable.form.extend!.cate = res.data.options
    })
}

</script>

<style scoped lang="scss"></style>
